package com.ruoyi.web.controller.dgw;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.dgw.domain.TagNumberExecl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.dgw.domain.TagNumber;
import com.ruoyi.dgw.service.ITagNumberService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 耳标Controller
 *
 * @author l
 * @date 2021-12-03
 */
@Api(tags="耳标相关接口")
@RestController
@RequestMapping("/dgw/tagNumber")
@Slf4j
public class TagNumberController extends BaseController {
    @Autowired
    private ITagNumberService tagNumberService;

    /**
     * 查询耳标列表
     */
    @PreAuthorize("@ss.hasPermi('dgw:tagNumber:list')")
    @GetMapping("/list")
    @ApiOperation("查询耳标列表")
    public TableDataInfo list(TagNumber tagNumber) {
        startPage();
        List<TagNumber> list = tagNumberService.selectTagNumberList(tagNumber);
        return getDataTable(list);
    }

    @Log(title = "耳标管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('dgw:tagNumber:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<TagNumber> util = new ExcelUtil<TagNumber>(TagNumber.class);
        List<TagNumber> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = tagNumberService.importTagNumber(userList, operName);
        return AjaxResult.success(message);
    }

    /**
     * 导出耳标列表
     */
    @PreAuthorize("@ss.hasPermi('dgw:tagNumber:export')")
    @Log(title = "耳标", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TagNumber tagNumber) {
        List<TagNumber> list = tagNumberService.selectTagNumberList(tagNumber);
        ExcelUtil<TagNumber> util = new ExcelUtil<TagNumber>(TagNumber.class);
        return util.exportExcel(list, "耳标数据");
    }

    @GetMapping("/importTemplate")
    @Log(title = "下载模板", businessType = BusinessType.INSERT)
    public AjaxResult importTemplate() {
        ExcelUtil<TagNumberExecl> util = new ExcelUtil<TagNumberExecl>(TagNumberExecl.class);
        log.info("导入的数据：{}", util);
        return util.importTemplateExcel("用户数据");
    }

    /**
     * 获取耳标详细信息
     */
    @PreAuthorize("@ss.hasPermi('dgw:tagNumber:query')")
    @GetMapping(value = "/{tagNumberId}")
    public AjaxResult getInfo(@PathVariable("tagNumberId") String tagNumberId) {
        return AjaxResult.success(tagNumberService.selectTagNumberByTagNumberId(tagNumberId));
    }

    /**
     * 新增耳标
     */
    @PreAuthorize("@ss.hasPermi('dgw:tagNumber:add')")
    @Log(title = "耳标", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TagNumber tagNumber) {
        return toAjax(tagNumberService.insertTagNumber(tagNumber));
    }

    /**
     * 修改耳标
     */
    @PreAuthorize("@ss.hasPermi('dgw:tagNumber:edit')")
    @Log(title = "耳标", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TagNumber tagNumber) {
        return toAjax(tagNumberService.updateTagNumber(tagNumber));
    }

    /**
     * 删除耳标
     */
    @PreAuthorize("@ss.hasPermi('dgw:tagNumber:remove')")
    @Log(title = "耳标", businessType = BusinessType.DELETE)
    @DeleteMapping("/{tagNumberIds}")
    public AjaxResult remove(@PathVariable String[] tagNumberIds) {
        return toAjax(tagNumberService.deleteTagNumberByTagNumberIds(tagNumberIds));
    }

    /**
    *app
    */
    @GetMapping("/queryBX")
    @ApiOperation("查询未买保险的耳标")
    public AjaxResult queryBX(TagNumber tagNumber){
        Long userId = getUserId();
        tagNumber.setUserId(Convert.toStr(userId));
        List<TagNumber> list = tagNumberService.queryBX(tagNumber);
        return AjaxResult.success(list);
    }

    @PutMapping("/updateEarNumber")
    public AjaxResult updateEarNumber(@RequestBody String tagNumber){
         tagNumberService.updateEarNumber(tagNumber);
        return AjaxResult.success("核销成功");
    }
    @PutMapping("/updateEar")
    public AjaxResult updateEar(String EarNumber, String earNumber){
        log.info("修改耳标：{},传入耳标:{}",EarNumber,earNumber);
        tagNumberService.updateEar(EarNumber,earNumber);
        return AjaxResult.success("替换成功");
    }


}
